from appium import webdriver
import time
from appium.options.common import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

option = AppiumOptions()
option.set_capability('platformName','android')
option.set_capability('platformVersion','7.1.2')
option.set_capability('deviceName','emulator-5554')
option.set_capability('appPackage','com.tencent.mm')
option.set_capability('appActivity','com.tencent.mm.ui.LauncherUI')
option.set_capability('noReset','true')
option.set_capability('newCommandTimeout','180')
option.set_capability("recreateChromeDriverSessions",'true')
# option.set_capability("browserName",'')
option.set_capability("chromeOptions",{"androidProcess": "com.tencent.mm:appbrand0"})
# option.set_capability("chromeOptions",{"androidProcess":"com.tencent.mm:toolsmp"})
driver = webdriver.Remote(command_executor='http://127.0.0.1:4723/wd/hub',options=option)

# desired_caps['chromeOptions'] = {'androidProcess': 'com.tencent.mm:appbrand0'}
time.sleep(10)
print('点击微信小程序')
# 切换到微信小程序的context
print('driver.current_context ---->',driver.current_context)
driver.switch_to.context('NATIVE_APP')
# 假设你要获取小程序中的某个按钮
button = driver.find_element(by=AppiumBy.XPATH, value="/html/body/wx-view/wx-view[3]/wx-uni-easyinput[1]/wx-view/wx-view/wx-input/div[1]/input")

# 执行动作，例如点击按钮
button.click()
time.sleep(10)
# driver.find_element(by=AppiumBy.XPATH, value="/html/body/wx-view/wx-view[3]/wx-uni-easyinput[1]/wx-view/wx-view/wx-input/div[1]/input").click()
# time.sleep(10)
driver.quit()
